package com.example.service.impl;


import com.example.common.Const;
import com.example.common.ServiceResponse;
import com.example.common.TableServerResponse;
import com.example.mapper.UserMapper;
import com.example.pojo.Manager;
import com.example.pojo.Percentage;
import com.example.pojo.User;


import com.example.service.IUserService;
import com.example.vo.SearchUserVO;
import com.example.vo.UserAllVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.JwtUtil;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sun.javafx.font.FontResource.SALT;

/**
 * @BelongsProject: gxcwhd
 * @BelongsPackage: com.example.service.impl
 * @Author: LJ
 * @CreateTime: 2020-08-13 14:03
 * @Description:
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public TableServerResponse getUserAll(Integer page, Integer pageSize) {
        //1.开启分页查询
        PageHelper.startPage(page, pageSize);
        //page = (page - 1)*pageSize;
        //2.执行查询操作
        List<UserAllVO> userAll = userMapper.getUserAll();
        //3.取出总共有多少条数据,使用分页实体类来取
        PageInfo pageInfo = new PageInfo(userAll);
        return TableServerResponse.createSuccess(0, userAll, pageInfo.getTotal());
    }

    @Override
    public TableServerResponse searchUser(SearchUserVO searchUserVO) {
        List<UserAllVO> list = userMapper.searchUser(searchUserVO);

        return TableServerResponse.createSuccess(list);
    }

    @Override
    public Map<String,Object> getPercentage() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("obj",userMapper.getPercentage());
        return map;
    }

    @Override
    public ServiceResponse updatePercentage(Percentage percentage) {
        Integer integer = userMapper.updatePercentage(percentage);
        if (integer > 0){
            return ServiceResponse.createSuccess();
        }else {
            return ServiceResponse.createError();
        }
    }

    @Override
    public ServiceResponse showUserInfo() {
        List<User> list = userMapper.showUserInfo();
        for(User user: list){
            if (user.getRoleId().equals("1")){
                user.setRoleId("出租户");
            }else if(user.getRoleId().equals("2")){
                user.setRoleId("租户");
            }
        }

        return ServiceResponse.createSuccess(list);
    }


}